package cn.cqzaier.pub.api;/**
 * @author: zehua
 * @date: 2022/5/16 13:24
 * @description:
 */

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.List;

/**
 * @author zehua
 * @date 2022/5/16
 */

public class Result<T>{
    public List<T> getResultArry(String jsonStr, Class clazz) {
        return  getResultArry(jsonStr,"result",clazz);
    }

    public List<T> getResultArry(String jsonStr,String resultkey, Class clazz) {
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        JSONArray jsonArray = (JSONArray)jsonObject.get(resultkey);
        return  jsonArray.toJavaList(clazz);
    }

    private final static String DONE=".";

    /**
     *
     *
     * json 多级路径
     * 点分割key
     * @param clazz
     * @param key
     * @return
     */
    public List<T> getResultArryByKeySplitDone(String json,Class clazz,String key) {
         if (StringUtils.isBlank(json)||StringUtils.isBlank(key)){
             return Collections.EMPTY_LIST;
         }
         String[] split = StringUtils.split(key,DONE);
        JSONObject jsonObject = JSONObject.parseObject(json);
        for (int i = 0; i < split.length; i++) {
            if (i == split.length - 1){
                JSONArray jsonArray = (JSONArray)jsonObject.get(split[i]);
                if (jsonArray == null )return Collections.EMPTY_LIST;
                return  jsonArray.toJavaList(clazz);
            }
            if (!jsonObject.containsKey(split[i]))return null;
            jsonObject =(JSONObject) jsonObject.get(split[i]);

        }
        return null;
    }


    public T getResultByKeySplitDone(String json,Class clazz,String key) {
        String[] split = StringUtils.split(key,DONE);
        JSONObject jsonObject = JSONObject.parseObject(json);
        for (int i = 0; i < split.length; i++) {
            if (i == split.length - 1){
                if (null == jsonObject)return null;
                JSONObject jsonObject2Clazz = (JSONObject)jsonObject.get(split[i]);
                return  (T)jsonObject2Clazz.toJavaObject(clazz);
            }
            if (!jsonObject.containsKey(split[i]))return null;
            jsonObject =(JSONObject) jsonObject.get(split[i]);

        }
        return null;
    }


    /**
     * 最后一层为String
     * @param json
     * @param clazz
     * @param key
     * @return
     */
    public T getResulString(String json,Class clazz,String key) {
        String[] split = StringUtils.split(key,DONE);
        JSONObject jsonObject = JSONObject.parseObject(json);
        for (int i = 0; i < split.length; i++) {
            if (i == split.length - 1){
                if (null == jsonObject)return null;
                if (String.class.isInstance(clazz)){
                    return  (T) jsonObject.getString(split[split.length - 1]);
                }
                if (Integer.class.isInstance(clazz)){
                    return  (T)Integer.valueOf( jsonObject.getString(split[split.length - 1]));
                }
            }
            if (!jsonObject.containsKey(split[i]))return null;
            jsonObject =(JSONObject) jsonObject.get(split[i]);
        }
        return null;
    }


    public T getResulType(String json,Class clazz,String... split) {
        JSONObject jsonObject = JSONObject.parseObject(json);
        for (int i = 0; i < split.length; i++) {
            if (i == split.length - 1){
                if (null == jsonObject)return null;
                if (clazz.isAssignableFrom(String.class)){
                    return  (T) jsonObject.getString(split[split.length - 1]);
                }
                if (clazz.isAssignableFrom(Integer.class)){
                    return  (T)Integer.valueOf( jsonObject.getString(split[split.length - 1]));
                }
            }
            if (!jsonObject.containsKey(split[i]))return null;
            jsonObject =(JSONObject) jsonObject.get(split[i]);
        }
        return null;
    }


}
